package by.exposit.service;

import by.exposit.dao.UserDAO;
import by.exposit.dao.entity.UserEntity;
import by.exposit.domain.User;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class UserServiceImpl implements UserService  {

    @Autowired
    private UserDAO userDAO;

    public UserServiceImpl() {
    }

    @Override
    public void saveUser(User user) {
         Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();

            userDAO.save(mapper.map(user, UserEntity.class));

    }

    @Override
    public List<UserEntity> userList() {
        return userDAO.getAll();
    }

    @Override
    public void removeUser(long userId) {
       userDAO.remove(userId);
    }



    @Override
    public User returnUserByLoginPassword(String login, String password) {

        Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
        UserEntity userEntity = userDAO.getUserByLoginPassword(login,password);
        User user = null;
        if(userEntity != null) {
            user = mapper.map(userEntity, User.class);
        }
        return user;
    }
}
